<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\constant\v1\CommonConstant;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1UserMoneyLog;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 资金记录 
 */
class V1UserMoneyLogController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需鉴权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];
    
    /**
     * @var V1UserMoneyLog
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1UserMoneyLog;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(Request $request): Response
    {
        $user_id = $request->input('user_id');
        $admin = admin();
        return view('v1-user-money-log/index',compact('user_id','admin'));
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            if(isset($where['user_id']) && $where['user_id'] > 0){
                // 用户端查看下级用户
            } else{
                // 管理员查看用户
                $where['user_id'] = $admin['id'];
            }
        }
        $where['deleted'] = CommonConstant::DELETE_0;
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-user-money-log/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-user-money-log/update');
    }


    /**
     * 订单
     * @param Request $request
     * @return Response
     * @throws BusinessException
     **/
    public function order(Request $request): Response{
        $id = $request->input('id');
        $type = $request->input('type');
        $template = 'v1-user-money-log/order_node';
        switch ($type){
            case CommonConstant::MONEY_LOG_TYPE_3:
                $template = 'v1-user-money-log/order_recharge';
                break;
            case CommonConstant::MONEY_LOG_TYPE_4:
            case CommonConstant::MONEY_LOG_TYPE_5:
                $template = 'v1-user-money-log/order_node';
                break;
        }
        $admin = admin();
        $user_status = $admin['user_status'];
        return view($template,compact('id','type','user_status'));
    }

}
